//Lindsay Ergenekan 
//ergenekl@onid.oregonstate.edu
//CS311-400
//Homework3 
//


#include <stdio.h> 
#include <getopt.h> 
#include <time.h> 
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/types.h>


#include <fcntl.h> 

 int demo(); 

int main(int argc, char **argv)
{ 
char c; 
struct stat s; 
int lflag=0;
int off1flag = 0; 
int off2flag = 0;
int fflag = 0;
int err = 0;
int length = 0;
int offset1 = 0;
int offset2 = 0;
char *file_name;
int inputFd, outputFd;
int numRead; 
int elength;
int eflag = 0;

while ((c = getopt(argc, argv, "f:o:O:l:e:v")) != -1)
    switch (c){ 
    case 'l': 
	lflag = 1;
        if (optarg != NULL)
        {
           length = atoi(optarg);
           if (length > 0)
           { 
              printf(" legnth of bytes to print out: %d    \n ", length); 
           }    
           else
           {
             printf(" Please enter in the number positive of bytes you would like to have printed out");
             exit (1);
           }
        }
        break;	
    case 'o': 
        off1flag = 1;
        offset1 = atoi(optarg);
        printf(" offset1: %d", offset1);
        break;
    case 'O':
        off2flag = 1;
        offset2 = atoi(optarg);
        printf(" offset2: %d", offset2);
        break;
    case 'f':
       fflag = 1;
       if (optarg != NULL)
       {
          file_name = optarg;
          printf("filename: %s,", file_name);
       }
       else { 
          printf("please enter a file name");
          exit(1); 
       }
       break;
    case 'e':
       eflag = 1;
       elength = atoi(optarg);
    case '?': 
        err = 1;
        printf ("Error is: %d", err);
        break;
    }


if (fflag == 1 && lflag == 1)
   {
   if (file_name != NULL)
   { 
     printf("Size of the file %s is %d bytes \n", file_name, (int) s.st_size);
   } 
   else
   {
     printf("You're still screwed.");
   }

    inputFd = open(file_name, O_RDONLY);
    int outputFd;

    ssize_t numRead;
    char buffer[length];
    printf("length: %d \n", length);

    printf("Test");

    if (off1flag == 1 )
    {
      printf("<OFFSET 1 >  ----------------------------------------------------------------- \n");
      lseek(inputFd, offset1, SEEK_SET);
    }
    else if (off2flag == 1 && off1flag == 0)
    {
      printf("<OFFSET 2 > ----------------------------------------------------------------- \n");
      lseek(inputFd, offset2, SEEK_CUR);
      off2flag = 0; 
    }
    else if (off1flag == 0 && off2flag == 0)
    { 
      printf("NEITHER OFFSET 2 NOR OFFSET 1 > ----------------------------------------------- \n");
      lseek(inputFd, 0, SEEK_SET);
    }

    numRead = read(inputFd, buffer, length);
    if (write(outputFd, buffer, length) != numRead)
    {
       printf("error");
    }
    printf("\n");   
//     printf("OFF2FLAG after writeout == %d \n", off2flag);
 
    if (off2flag == 1)
    {
     lseek(inputFd, offset2, SEEK_CUR);
     char buffer[length]; 
     printf("<OFFSET 2 > ------------------------------------------------------------- \n");
    numRead = read(inputFd, buffer, length);
      if (write(outputFd, buffer, length) != numRead)
      {
         printf("error");
      }

    }

    if (eflag == 1)
    {
       printf("elength == %d \n", elength);
       lseek(inputFd, -(elength), SEEK_END);
       char buffer [elength];
       printf("<End Length> ------------------------------------------------------------- \n");
       numRead = read(inputFd, buffer, elength);
       if (write(outputFd, buffer, elength) != numRead)
       {
         printf("error");
       }
    }
    
  }
return 0; 
}

